package cn.echase.ecare.yinfayun.controller.access;

import cn.echase.common.core.exception.EchaseException;
import cn.echase.ecare.yinfayun.domain.dto.AssessInfoDto;
import cn.echase.ecare.yinfayun.domain.vo.AssesElderMedicineVo;
import cn.echase.ecare.yinfayun.domain.vo.AssessInfoVo;
import cn.echase.ecare.yinfayun.service.IAssessInfoService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static cn.echase.ecare.yinfayun.constants.AssessConstant.TemplateContentKey.KEY_subform73888;

/**
 * 评估报告管理
 *
 * @author lidisong
 * @date 2024-11-12
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/assess/assessReport")
public class AssessReportController {

    private final IAssessInfoService assessInfoService;

    /**
     * 根据评估编号查询评估打印信息（新）
     * */
    @RequestMapping(value = "getAssessInfo", method = RequestMethod.POST)
    public JSONObject getAssessInfo(@RequestBody AssessInfoDto assessInfoDto)  throws Exception{
        if (StringUtils.isEmpty(assessInfoDto.getAssessNo())) {
            throw new EchaseException(500, "assessNo不能为空");
        }
        return assessInfoService.queryAssessPrintData(assessInfoDto.getAssessNo(),null);
    }

    /**
     * 人员评估情况中的服药情况（新）
     * */
    @RequestMapping(value = "getMedicineList", method = RequestMethod.POST)
    public JSONObject getMedicineList(@RequestBody AssessInfoDto assessInfoDto) throws Exception {
        if (StringUtils.isEmpty(assessInfoDto.getAssessNo())) {
            throw new EchaseException(500, "assessNo不能为空");
        }
        JSONObject json = assessInfoService.queryAssessPrintData(assessInfoDto.getAssessNo(), KEY_subform73888);
        log.info("获取结果{}",json);
        JSONArray dataArray = json.getJSONArray("data");
        AssesElderMedicineVo medicineVo = assessInfoService.initAssesElderMedicineVo(dataArray);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", medicineVo);
        return jsonObject;
    }

    /**
     * 获取人员的评估信息（新）
     * */
    @RequestMapping(value = "getAssessInfoAndEldInfo", method = RequestMethod.POST)
    public JSONObject getAssessInfoAndEldInfo( @RequestBody AssessInfoDto assessInfoDto) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(assessInfoDto.getAssessNo())) {
            throw new EchaseException(500, "assessNo不能为空");
        }
        AssessInfoVo assessInfoVo =  assessInfoService.queryByAssessNo(assessInfoDto);
        jsonObject.put("data", assessInfoVo);
        return jsonObject;
    }

}
